// pages/wages/wages.js
import Toast from '@vant/weapp/toast/toast';
const computedBehavior = require("miniprogram-computed").behavior;
import Dialog from '@vant/weapp/dialog/dialog';
let {
  toFixed,
  sbjs,
  gjjjs,
  gsjs
}=require('./utils/index')
Page({
  behaviors: [computedBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    gjj:0,
    gjjtotal:0,
    medical:0,
    endowment:0,
    unemployment:0,
    gjjmenoy:'',
    gjjChecked:true,
    sbmenoy:'',
    sbChecked:true,
    moneyValue:'',
  money:0,
  tax:0,
  index:0,
  curcity:'北京',
  socialSecurity:0,
  accumulationFund:0,
  array:['北京','上海','广州','深圳','杭州','西安','南京','苏州', '合肥','武汉','宁波','天津','厦门','福州','南昌','济南','郑州','长沙','重庆','成都','青岛','东莞'],
  gjjindex:0,
  gjjarray:[12,11,10,9,8,7,6,5],
  arr:{
    //第一个是公积金上限，第二个是社保上限
    '北京':[31884,31884],
    '上海':[34188,34188],
    '广州':[36072,24930],
    '深圳':[34860,11620],
    '杭州':[32077,16608],
    '苏州':[31600,21821],
    '西安':[27770,19710],
    '南京':[37200,21821], 
    '合肥':[24207,21821],
    '武汉':[29330,20385],
    '成都':[27790,27790],
    '天津':[29730,22434],
    '郑州':[24091,17043],
    '长沙':[28700,17931],
    '重庆':[26742,19784],
    '济南':[28650,16346],
    '厦门':[28884,19962],
    '福州':[22239,18379],
    '南昌':[25521,16644],
    '宁波':[30972,19070],
    '青岛':[29119,19899],
    '东莞':[24753,19899],
  },
  gjjsb:[31884,31884],
  gjjtqChecked:false,
  },
  //公积金缴纳比例改变
  gjjPickerChange:function(e){
    let nowIndex=e.detail.value
    this.setData({
      gjjindex:nowIndex,
    })
    let bl=Number(this.data.gjjarray[this.data.gjjindex])
    let {
      gjj,
      gjjtotal
    }=gjjjs(this.data.moneyValue,bl)
    this.setData({
      gjj,
      gjjtotal
    })
    let {
      ds,
      gs
    }=gsjs(this.data.moneyValue,this.data.socialSecurity,gjj)
     this.setData({
       money:ds,
       tax:gs
     })
  },
 //公积金基数改变
  gjjonChange:function(e){
    let value=e.detail==''?0:Number(e.detail)
    let sx=this.data.arr[this.data.array[this.data.index]][0]
    let moneyValue=this.data.moneyValue
    if(value>=moneyValue&&value<sx||value>=moneyValue&&value>=sx){
      //如果value比上限和税前工资都大就取最小的
     value=moneyValue<=sx?moneyValue:sx
      this.setData({
        gjjmenoy:moneyValue
      })
    }else
    if(value>=sx){
      value=sx
      this.setData({
        gjjmenoy:value
      })
      Toast('已经到达公积金基数上限');
    }
    let bl=Number(this.data.gjjarray[this.data.gjjindex])
    let {
      gjj,
      gjjtotal
    }=gjjjs(value,bl)
    this.setData({
      gjj,
      gjjtotal
    })
    let {
      ds,
      gs
    }=gsjs(this.data.moneyValue,this.data.socialSecurity,gjj)
     this.setData({
       money:ds,
       tax:gs
     })
   
  },
  //是否缴纳社保
  sbswChange:function(e){
    this.setData({ 
      sbChecked: e.detail,
      socialSecurity:0,
      unemployment:0,
      endowment:0,
      medical:0,
      sbmenoy:'',
     });
     let {ds,gs}=gsjs(this.data.moneyValue,this.data.socialSecurity,this.data.gjj)
     this.setData({
       tax:gs,
       money:ds,
     })

  },
  //是否缴纳公积金
  gjjswChange:function(e){
    this.setData({ 
      gjjChecked: e.detail,
      gjj:0,
      gjjmenoy:0,
      gjjtotal:0
    });
    let {
      ds,
      gs
    }=gsjs(this.data.moneyValue,this.data.socialSecurity,this.data.gjj)
    this.setData({
      money:ds,
      tax:gs
    })
  },
  //选择城市
  bindPickerChange: function(e) {
    let nowIndex=e.detail.value
    this.setData({
      index: nowIndex,
      curcity:this.data.array[nowIndex],
      gjjsb:this.data.arr[this.data.array[nowIndex]]
    })
    this.moneyonChange({
      detail:this.data.moneyValue
    })

    
  },
  //税前工资改变
  moneyonChange:function(e){
    let money=e.detail
    if(money[0]=='0'){
      this.setData({
        moneyValue:''
      })
      return
    }
    this.setData({
      moneyValue:money,
      })
//社保设置
let sbsx=0,sx=this.data.arr[this.data.array[this.data.index]][1]
if(money>sx){
 sbsx=sx
  Toast('已经到达社保基数上限');
}else{
  sbsx=money
}

let {
  //总社保
  socialSecurity,
  //医疗
  medical,
  //养老
  endowment,
  //失业
  unemployment,
}=sbjs(sbsx)
this.setData({
  socialSecurity,
  medical,
  endowment,
  unemployment,
  sbmenoy:sbsx
})
//公积金设置
let gjjsx=0,sxg=this.data.arr[this.data.array[this.data.index]][0]
if(money>sxg){
    gjjsx=sxg
   Toast('已经到达公积金基数上限');
 }else{
   gjjsx=money
 }
 let bl=this.data.gjjarray[this.data.gjjindex]
 let {
   gjj,
   gjjtotal
 }=gjjjs(gjjsx,bl)
this.setData({
  gjj,
  gjjtotal,
  gjjmenoy:gjjsx
})
if(!this.data.sbChecked)socialSecurity=0
if(!this.data.gjjChecked)gjj=0
let {ds,gs}=gsjs(money,socialSecurity,gjj)
this.setData({
  money:ds,
  tax:gs
})
  },

  //社保基数改变
  sbonChange(e) {
    // 需要手动对 checked 状态进行更新
    let value=e.detail==''?0:Number(e.detail)
    let sx=this.data.arr[this.data.array[this.data.index]][1]
    let moneyValue=this.data.moneyValue
    if(value>=moneyValue&&value<sx||value>=moneyValue&&value>=sx){
     value=moneyValue<=sx?moneyValue:sx
      this.setData({
        sbmenoy:moneyValue
      })
    }else
    if(value>=sx){
      value=sx
      this.setData({
        sbmenoy:value
      })
      Toast('已经到达社保基数上限');
    }
   let {
  socialSecurity,
  medical,
  endowment,
  unemployment,
}=sbjs(value)
let {ds,gs}=gsjs(this.data.moneyValue,socialSecurity,this.data.gjj)
    this.setData({
      socialSecurity,
      medical,
      endowment,
      unemployment,
      money:ds,
      tax:gs
    })
    
  },
  gotogjj(){
    wx.navigateTo({
      url: '../gjj/gjj?city='+this.data.array[this.data.index],
    })
  },
  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    Toast('初始默认为北京');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})